import CryptoJs from 'crypto-js'

const dic = {
  '1': '$',
  '2': '@',
  '3': '#',
  '4': '!',
  '5': '%',
  '6': '^',
  '7': '(',
  '8': '*',
  '9': '&',
  '0': ')'
}

export default {
  encrypt (text) {
    let pre = ''
    let suf = ''
    if (text.length === 6) {
      pre = text
    } else {
      pre = text.substr(0, 6)
      suf = text.substr(6)
    }
    return this.map2Symbol(this.reverse(pre + CryptoJs.MD5(text).toString() + suf))
  },
  reverse (text) {
    return text.split('').reverse().join('')
  },
  map2Symbol (text) {
    let sb = []
    for (let i = 0; i < text.length; i++) {
      let char = text.charAt(i)
      if (dic[char] !== undefined) {
        sb.push(dic[char])
      } else {
        sb.push(char)
      }
    }
    return sb.join('')
  }
}
